package com.anji.util;

import com.anji.integration.Activator;

public class ActivationFacade implements Activator {

	private float minResponse;
	private float maxResponse;
	private int[] inDim;
	private int[] outDim;
	
	public ActivationFacade(int[] inDim, int[] outDim, float minResponse, float maxResponse) {
		this.inDim = inDim;
		this.outDim = outDim;
		this.minResponse = minResponse;
		this.maxResponse = maxResponse;
	}
	
	@Override
	public Object next() {
		return null;
	}

	@Override
	public float[] next(float[] stimuli) {
		return produceOutput(stimuli);
	}

	@Override
	public float[][] nextSequence(float[][] stimuli) {
		float[][] a = new float[stimuli.length][];
		for (int i = 0; i < a.length; i++) 
			a[i] = produceOutput(stimuli[i]);
		return a;
	}

	@Override
	public float[][] next(float[][] stimuli) {
		return produceOutput(stimuli);
	}

	@Override
	public float[][][] nextSequence(float[][][] stimuli) {
		float[][][] a = new float[stimuli.length][][];
		for (int i = 0; i < a.length; i++) 
			a[i] = produceOutput(stimuli[i]);
		return a;
	}

	private float[] produceOutput(float[] stimuli) {
		// Assume 1D output array
		float[] out = new float[outDim[0]];
		for (int i = 0; i < out.length; i++) out[i] = rnd();
		return out;
	}
	
	private float[][] produceOutput(float[][] stimuli) {
		// Assume 2D output array
		float[][] out = new float[outDim[0]][outDim[1]];
		for (int i = 0; i < out.length; i++)
			for (int j = 0; j < out[i].length; j++)	
				out[i][j] = rnd();
		return out;
	}
	
	private float rnd() {
		float r = (float) Math.random();
		r *= maxResponse - minResponse;
		r += minResponse;
		return r;
	}
	
	@Override
	public String getXmlRootTag() {return "activationfacade";}
	@Override
	public String getXmld() {return getName();}
	@Override
	public String toXml() {return "<activationfacade/>";}
	@Override
	public void reset() {}
	@Override
	public String getName() {return "activationfacade";}
	@Override
	public float getMinResponse() {return minResponse;}
	@Override
	public float getMaxResponse() {return maxResponse;}
	@Override
	public int[] getInputDimension() {return inDim;}
	@Override
	public int[] getOutputDimension() {return outDim;}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		ActivationFacade f = new ActivationFacade(new int[] {4, 5}, new int[] {5,6}, 2, 5);
		
		float[][][] a = new float[][][] {{{0},{0}},{{0},{0}}};
		System.out.println(f.nextSequence(a));
		System.out.println(f.nextSequence(a));
		System.out.println(f.nextSequence(a));
	}

}
